package com.zenithsun.common.image;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import com.syzg.common.util.PKCreator;
import com.syzg.core.util.DateUtil;

/**
 * 文件存储规则。<br/>
 * 
 * @author wanglei@enersun.com.cn
 * @createDate 2016-7-4 15:22:30
 */
public class StorageRule {

	/**
	 * 是否临时存储
	 */
	private boolean tempStore = false;

	/**
	 * 文件名称
	 */
	private String fileName;

	/**
	 * 文件名称, uuid格式
	 */
	private String UUIDFileName;

	/**
	 * 当前工程绝对位置
	 */
	private String currProjectPath;

	/**
	 * 数据库存储的相对路径
	 */
	private String relativeStorePath;

	public String getRelativeStorePath() {
		if(relativeStorePath==null){
			this.setRelativeStorePath(this.createRelativePath());
		}
		return relativeStorePath;
	}

	public void setRelativeStorePath(String relativeStorePath) {
		this.relativeStorePath = relativeStorePath;
	}

	/**
	 * 新闻图片存储地址（临时）
	 */
	public static String newsTempSavePath = "/zenithsun/news/tempimages/";

	/**
	 * 新闻图片存储地址（真实）
	 */
	public static String newsSavePath = "/zenithsun/news/images/";

	/**
	 * 图片库图片存储地址（临时）
	 */
	public static String imageTempSavePath = "/zenithsun/pic/tempimages/";

	/**
	 * 图片库图片存储地址（真实）
	 */
	public static String imageSavePath = "/zenithsun/pic/images/";
	
	/**
	 * app存储地址（真实）
	 */
	public static String appSavePath = "/zenithsun/app/";
	
	private HttpServletRequest request;

	public StorageRule() {

	}

	public StorageRule(HttpServletRequest request) {
		this.request = request;
		// 设置当前项目实际存储路径
		this.setCurrProjectPath(request.getServletContext().getRealPath("").replaceAll("\\\\", "/"));
	}

	public StorageRule(String fileName, String currProjectPath) {
		this.setFileName(fileName);
		this.currProjectPath = currProjectPath;
	}

	public static void main(String[] args) {
	}

	/**
	 * 获取APP存储地址
	 * @return
	 */
	public String GetAppStorePath(){
		StringBuffer sb = new StringBuffer();
		if (this.currProjectPath != null) {
			sb.append(this.currProjectPath);
		}
		sb.append(appSavePath).append(this.fileName);
		return sb.toString().replaceAll("\\\\", "/");
	}
	
	/**
	 * 获取图片库图片存储路径（绝对路径）
	 * 
	 * @param fileName
	 * @return String
	 */
	public String GetPicImageStorePath() {
		StringBuffer sb = new StringBuffer();
		if (this.currProjectPath != null) {
			sb.append(this.currProjectPath);
		}
		if (this.isTempStore()) {
			sb.append(imageTempSavePath);
			if(null==getUUIDFileName()){
				sb.append(PKCreator.getPk());
			}else{
				sb.append(getUUIDFileName());
			}
		} else {
			//按规则创建目录
			this.setRelativeStorePath(this.createRelativePath());
			sb.append(imageSavePath);
			sb.append(this.getRelativeStorePath());
		}
		return sb.toString().replaceAll("\\\\", "/");
	}

	/**
	 * 创建新的文件名称，格式为uuid.后缀
	 * 
	 * @return String
	 */
	private String creatUUIDName() {
		StringBuffer sb = new StringBuffer();
		sb.append(PKCreator.getPk()).append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}
	/**
	 * 创建新的文件名称，格式为uuid.后缀
	 * @return String
	 */
	public static String creatUUIDName(String fileName) {
		StringBuffer sb = new StringBuffer();
		sb.append(PKCreator.getPk()).append(fileName.substring(fileName.lastIndexOf(".")));
		return sb.toString();
	}
	/**
	 * 获取新闻图片存储路径（绝对路径）
	 * 
	 * @param fileName
	 * @return String
	 */
	public String GetNewsImageStorePath() {
		StringBuffer sb = new StringBuffer();
		if (this.currProjectPath != null) {
			sb.append(this.currProjectPath);
		}
		if (this.isTempStore()) {
			sb.append(newsTempSavePath);
			if(null==getUUIDFileName()){
				sb.append(PKCreator.getPk());
			}else{
				sb.append(getUUIDFileName());
			}
		} else {
			//按规则创建目录
			this.setRelativeStorePath(this.createRelativePath());
			sb.append(newsSavePath);
			sb.append(this.getRelativeStorePath());
		}
		return sb.toString().replaceAll("\\\\", "/");
	}

	/**
	 * 获取新闻图片存储路径（相对路径）
	 * 
	 * @param request
	 * @return String
	 */
	public String GetNewsImageShowPath() {
		StringBuffer sb = new StringBuffer();
		/*
		sb.append(request.getScheme()).append("://")
				.append(request.getServerName()).append(":")
				.append(request.getServerPort())
				.append(request.getContextPath());
		*/
		sb.append(request.getContextPath());
		if (this.isTempStore()) {
			sb.append(newsTempSavePath);
		} else {
			sb.append(newsSavePath);
		}
		return sb.toString().replaceAll("\\\\", "/");
	}

	/**
	 * 获取图片库图片存储路径（相对路径）
	 * 
	 * @param request
	 * @return String
	 */
	public String GetPicImageShowPath() {
		StringBuffer sb = new StringBuffer();
		/*
		sb.append(request.getScheme()).append("://")
				.append(request.getServerName()).append(":")
				.append(request.getServerPort())
				.append(request.getContextPath());
				*/
		sb.append(request.getContextPath());
		if (this.isTempStore()) {
			sb.append(imageTempSavePath);
		} else {
			sb.append(imageSavePath);
		}
		return sb.toString().replaceAll("\\\\", "/");
	}
	
	/**
	 * 获取当前项目的HTTP访问路径
	 * @return String
	 */
	public String GetCurrProjectPathHttpPath() {
		StringBuffer sb = new StringBuffer();
		/*
		sb.append(request.getScheme()).append("://")
				.append(request.getServerName()).append(":")
				.append(request.getServerPort())
				.append(request.getContextPath());
				*/
		sb.append(request.getContextPath());
		return sb.toString().replaceAll("\\\\", "/");
	}
	
	/**
	 * 获取当前项目的HTTP访问路径(全路径)
	 * @return String
	 */
	public String GetFullCurrProjectPathHttpPath() {
		StringBuffer sb = new StringBuffer();
		sb.append(request.getScheme()).append("://")
				.append(request.getServerName()).append(":")
				.append(request.getServerPort())
				.append(request.getContextPath());
		return sb.toString().replaceAll("\\\\", "/");
	}
	
	/**
	 * 获取某个路径在当前项目中的HTTP访问路径
	 * @param path
	 * @return String
	 */
	public String GetCurrProjectPathHttpPath(String path) {
		return this.GetCurrProjectPathHttpPath()+path.replaceAll(this.getCurrProjectPath(), "");
	}
	/**
	 * 创建相对路径，按规则创建
	 * @return
	 */
	private String createRelativePath() {
		StringBuffer sb = new StringBuffer();
		sb.append(DateUtil.format(new Date(), "yyyy/MMdd")).append("/").append(this.getUUIDFileName());
		return sb.toString();
	}

	public String getUUIDFileName() {
		return UUIDFileName;
	}

	public void setUUIDFileName(String uUIDFileName) {
		UUIDFileName = uUIDFileName;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		this.setUUIDFileName(this.creatUUIDName());
	}

	public String getCurrProjectPath() {
		return currProjectPath;
	}

	public void setCurrProjectPath(String currProjectPath) {
		this.currProjectPath = currProjectPath;
	}

	public boolean isTempStore() {
		return tempStore;
	}

	public void setTempStore(boolean tempStore) {
		this.tempStore = tempStore;
	}
}